// ==UserScript==
// @name           DRL Toolkit
// @description    DRL 辅助功能
// @include      https://d4e.org/*
// @include      https://cnc.d4e.org/*
// @include      https://sh.d4e.org/*
// @include      https://www.d4e.org/*
// @include      https://dream4ever.org/*
// @include      https://c.dream4ever.org/*
// @include      https://cnc.dream4ever.org/*
// @include      https://cnc2.dream4ever.org/*
// @include      https://cnc2.mirror.dream4ever.org/*
// @include      https://cs.dream4ever.org/*
// @include      https://mirror.dream4ever.org/*
// @include      https://sc.dream4ever.org/*
// @include      https://sh.dream4ever.org/*
// @include      https://www.dream4ever.org/*
// ==/UserScript==




// 选项初始化
if (GM_getValue('isPuhui') == undefined) GM_setValue('isPuhui', true);// 普会或普会以上级别为true，否则为false
if (GM_getValue('resolveAccessKeyConflict') == undefined)   GM_setValue('resolveAccessKeyConflict', true);
if (GM_getValue('addFtpRelatedItems') == undefined)   GM_setValue('addFtpRelatedItems', true);
if (GM_getValue('ignoredMembersInDrl') == undefined)   GM_setValue('ignoredMembersInDrl', '');
GM_deleteValue('alreadyRun'); // remove a legacy setting


/** 解决帖子编辑页面中的快捷键冲突 **/

var resolveAccessKeyConflict = GM_getValue('resolveAccessKeyConflict', true);
if (resolveAccessKeyConflict)
{

    var inputElements = document.getElementsByTagName("input");
    var tag = 0;
    if (inputElements.length)
    {
	for (i = 0; i<inputElements.length; i++)
	{
	    switch (inputElements[i].value)
	    {
		case "删除帖子":
		    inputElements[i].setAttribute("accesskey","d");
		    continue;
		case "保存修改":
		case "发表新主题":
		case "发表回复":
		case "预览":
		case "预览修改效果":
		    if (2 > tag) tag += 1;
		    else inputElements[i].removeAttribute("accesskey");
		    continue;
		default:
		    continue;
	    }
	}
    }
}



/** 在快捷功能菜单中添加 FTP 项目 **/

var addFtpRelatedItems = GM_getValue('addFtpRelatedItems', true);
if (addFtpRelatedItems)
{
    var isPuhui = GM_getValue('isPuhui', true);
    var menus = document.getElementById("usercptools_menu").getElementsByTagName("tbody");
    var ftpIndexPostId = (isPuhui)?"82097":"15601";
    var ftpSearchEngineUrlSuffix = (isPuhui)?"PuHui":"ChuHui";
    var appendedItem = new Array();
    appendedItem[0] = document.createElement("tr");
    appendedItem[1] = document.createElement("tr");
    appendedItem[0] = menus[0].appendChild(appendedItem[0]);
    appendedItem[0].innerHTML = '<td style="cursor: pointer;" class="vbmenu_option"><a href="showthread.php?t='+ftpIndexPostId+'">FTP 总索引</a></td>';
    appendedItem[1] = menus[0].appendChild(appendedItem[1]);
    appendedItem[1].innerHTML = '<td style="cursor: pointer;" class="vbmenu_option"><a href="http://noctwolf.dream4ever.org/'+ftpSearchEngineUrlSuffix+'/">FTP 搜索</a></td>';
}

/** 隐藏被忽略的用户的主题与帖子 **/

function isInArray (string, array)
{
    for (j = 0; j<array.length; j++ )
    {
	if ( string === array[j])
	{
	    return true;
	}
    }
    return false;
}


var ignoredMembersInDrl = GM_getValue('ignoredMembersInDrl', '');
if ('' != ignoredMembersInDrl)
{
    ignoredMembersInDrl = ignoredMembersInDrl.split(";");
    var spans = document.getElementsByTagName("span");
    var names = document.getElementsByClassName("bigusername");
    for (i=0; i<spans.length; i++)
    {
	if (isInArray(spans[i].innerHTML, ignoredMembersInDrl))
	    spans[i].parentNode.parentNode.parentNode.style.display = "none";
    }
    for (i=0; i<names.length; i++)
    {
	if (isInArray(names[i].innerHTML.toString().replace(/<\/?[^>]*>/gi,""), ignoredMembersInDrl)){
	    theParent = names[i];
	    while (theParent.parentNode.id!="posts")
	    {
		theParent = theParent.parentNode
	    }
	    theParent.style.display = "none";
	}
    }
}